<!--  -->
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>上传文件测试</title>
    </head>
    <body>
        <input id="file-selector" type="file">
<script src="/static/store/cos/cos-js-sdk-v5.js"></script>
<script>
var Bucket = 'cq-1252015630';
var Region = 'ap-chongqing';

// 初始化实例
var cos = new COS({
    getAuthorization: function (options, callback) {
        var url = 'https://i.fe80.cn/tools/sdk/sts.html';
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onload = function (e) {
            try {
                var data = JSON.parse(e.target.responseText);
            } catch (e) {
            }
            callback({
                TmpSecretId: data.credentials && data.credentials.tmpSecretId,
                TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
                XCosSecurityToken: data.credentials && data.credentials.sessionToken,
                ExpiredTime: data.expiredTime,
            });
        };
        xhr.send();
    }
});



// 监听选文件
document.getElementById('file-selector').onchange = function () {

    var file = this.files[0];
    if (!file) return;

    // 小文件上传
    cos.putObject({
        Bucket: Bucket,
        Region: Region,
        Key: 'images/' + file.name,
        StorageClass: 'STANDARD',
        Body: file, // 上传文件对象
        onProgress: function(progressData) {
            console.log(JSON.stringify(progressData));
        }
    }, function(err, data) {
        console.log(err || data);
    });

    // 分片上传文件
    // cos.sliceUploadFile({
    //     Bucket: Bucket,
    //     Region: Region,
    //     Key: file.name,
    //     Body: file,
    //     onHashProgress: function (progressData) {
    //         console.log('校验中', JSON.stringify(progressData));
    //     },
    //     onProgress: function (progressData) {
    //         console.log('上传中', JSON.stringify(progressData));
    //     },
    // }, function (err, data) {
    //     console.log(err, data);
    // });

};
</script>
    </body>
</html>
